﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO
{
    public class FriendshipDAO
    {
        WebChatDBEntities db = new WebChatDBEntities();

        public IEnumerable<User> GetFriendByUserId(int userId)
        {
            var friends = new List<User>();
            var friendships = db.Friendships.Where(f => (f.UserId == userId));
            foreach (var friendship in friendships)
            {
                var user = db.Users.Find(friendship.FriendId);
                friends.Add(user);
            }

            friendships = db.Friendships.Where(f => (f.FriendId == userId));
            foreach (var friendship in friendships)
            {
                var user = db.Users.Find(friendship.UserId);
                friends.Add(user);
            }

            return friends.AsEnumerable();
        }

        public bool AddFriendship(int userId, string friendName)
        {
            var user = db.Users.Find(userId);
            var friend = db.Users.SingleOrDefault(u => u.UserName.Equals(friendName));

            if (user != null && friend != null)
            {
                Friendship friendship = new Friendship
                {
                    UserId = user.Id,
                    FriendId = friend.Id
                };
                db.Friendships.Add(friendship);

                FriendRequest request =
                    db.FriendRequests.FirstOrDefault(r => r.UserId == friend.Id && r.ToUserId == user.Id);
                db.FriendRequests.Remove(request);

                db.SaveChanges();

                return true;
            }
            
            return false;
        }

        public bool DeleteFriendship(int userId, string friendName)
        {
            var user = db.Users.Find(userId);
            var friend = db.Users.SingleOrDefault(u => u.UserName.Equals(friendName));

            if (user == null || friend == null)
            {
                return false;
            }

            Friendship friendship = user.Friendships.SingleOrDefault(f => f.FriendId == friend.Id);
            if (friendship != null)
            {
                db.Friendships.Remove(friendship);
                db.SaveChanges();
            }
            else
            {
                friendship = friend.Friendships.SingleOrDefault(f => f.FriendId == user.Id);
                if (friendship != null)
                {
                    db.Friendships.Remove(friendship);
                    db.SaveChanges();
                }
            }

            return true;
        }

        public bool ImportFriend(string userName, string password, int userId)
        {
            User user = db.Users.SingleOrDefault(u => u.UserName.Equals(userName) && u.Password.Equals(password));
            if(user == null)
                return false;

            List<int> friendsId = GetFriendByUserId(user.Id).Select(f => f.Id).ToList();
            List<int> currentFriendsId = GetFriendByUserId(userId).Select(f => f.Id).ToList();
            foreach (var friendId in friendsId)
            {
                if(currentFriendsId.Contains(friendId))
                    continue;

                Friendship friendship = new Friendship
                                        {
                                            UserId = userId,
                                            FriendId = friendId
                                        };

                db.Friendships.Add(friendship);
            }

            db.SaveChanges();
            return true;
        }
    }
}
